#include <iostream>
#include <fstream>
#include <sstream>
#include <stdlib.h>
using namespace std;

#include "types.h"
#include "base64.h"


bool isBase64(uchar c)
{
	if ((c=='+') || (c=='/') || 
			((c>='a') && (c<='z')) || 
			((c>='A') && (c<='Z')) || 
			((c>='0'&&c<='9')))
		return TRUE;
	else
		return FALSE;
}


uchar bitsToChar(const char *input)
{
	uchar c = '\0';
	if (input)
	{
		for (int i = 0; i < 8; i++)
		{
			c = c*2 + input[i]-'0';
		}
	}
	return c;
}

string charToBits(uchar c)
{
	string bits;
	int i;

	for (i = 7; i >= 0; i--)
		bits += (c & (1 << i)) ? '1' : '0';

	return bits;
}

string intToString(int i)
{
	stringstream ss; 
	ss << i;
	return ss.str();
}

int stringToInt(string str)
{
	stringstream ss(str);
	int i;

	if (!(ss >> i))
		i = 0;

	return i;
}


string getRandomString(uint len)
{
	string str;

	// initialize random seed.
	srand ( time(NULL) );
	
	for (; len > 0; len--)
	{
		// generate secret number
		str += (char)(rand() % 255 + 1);
	}
	return str;
}
